package com.tqyan.service.service.impl;

import com.tqyan.dal.mapper.ChatContentMapper;
import com.tqyan.dal.mapper.CopyInfoMapper;
import com.tqyan.model.dto.ChatContentDTO;
import com.tqyan.model.dto.CopyInfoDTO;
import com.tqyan.model.dto.UserDTO;
import com.tqyan.model.enums.ChatTypeEnum;
import com.tqyan.service.service.ChatService;
import com.tqyan.service.service.InfoService;
import com.tqyan.service.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 信息服务
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/4/17
 */
@Service
@Slf4j
public class InfoServiceImpl implements InfoService {
    private static final Map<Integer, CopyInfoDTO> POINT_CHAT_MAP = new HashMap<>();
    private static final Map<Integer, CopyInfoDTO> POINT_DRAW_MAP = new HashMap<>();
    private static final Map<Integer, CopyInfoDTO> TIPS_MAP = new HashMap<>();
    private static final Map<Integer, CopyInfoDTO> SHARE_CONTENT_MAP = new HashMap<>();
    private static final Map<Integer, CopyInfoDTO> SHARE_IMAGE_MAP = new HashMap<>();

    @Autowired
    private CopyInfoMapper copyInfoMapper;
    @Autowired
    private ChatContentMapper chatContentMapper;

    @Autowired
    private UserService userService;

    /**
     * @description 初始化会话指引、小贴士
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/4/12 10:51
     */
    @PostConstruct
    @Override
    public void init() {
        // 聊天指引
        List<CopyInfoDTO> copyInfoList = copyInfoMapper.queryPointListByChat();
        int currWeight = 0;
        for (CopyInfoDTO copyInfo : copyInfoList) {
            if (null == copyInfo) {
                continue;
            }
            for (int weight = 0; weight < copyInfo.getWeight(); weight++) {
                POINT_CHAT_MAP.put(currWeight + weight, copyInfo);
            }
            currWeight += copyInfo.getWeight();
        }
        log.info("[初始化] 聊天指引: {}", POINT_CHAT_MAP.size());

        // 绘画指引
        copyInfoList = copyInfoMapper.queryPointListByDraw();
        currWeight = 0;
        for (CopyInfoDTO copyInfo : copyInfoList) {
            if (null == copyInfo) {
                continue;
            }
            for (int weight = 0; weight < copyInfo.getWeight(); weight++) {
                POINT_DRAW_MAP.put(currWeight + weight, copyInfo);
            }
            currWeight += copyInfo.getWeight();
        }
        log.info("[初始化] 绘画指引: {}", POINT_DRAW_MAP.size());

        // 小贴士
        copyInfoList = copyInfoMapper.queryListByTips();
        currWeight = 0;
        for (CopyInfoDTO copyInfo : copyInfoList) {
            if (null == copyInfo) {
                continue;
            }
            for (int weight = 0; weight < copyInfo.getWeight(); weight++) {
                TIPS_MAP.put(currWeight + weight, copyInfo);
            }
            currWeight += copyInfo.getWeight();
        }
        log.info("[初始化] 小贴士: {}", TIPS_MAP.size());

        // 分享
        copyInfoList = copyInfoMapper.queryListByShare();
        // 内容
        currWeight = 0;
        for (CopyInfoDTO copyInfo : copyInfoList) {
            if (null == copyInfo) {
                continue;
            }
            for (int weight = 0; weight < copyInfo.getWeight(); weight++) {
                SHARE_CONTENT_MAP.put(currWeight + weight, copyInfo);
            }
            currWeight += copyInfo.getWeight();
        }
        // 图片
        currWeight = 0;
        for (CopyInfoDTO copyInfo : copyInfoList) {
            if (null == copyInfo || StringUtils.isBlank(copyInfo.getImageUrl())) {
                continue;
            }
            SHARE_IMAGE_MAP.put(currWeight++, copyInfo);
        }
        log.info("[初始化] 分享, 内容: {}, 图片: {}", SHARE_CONTENT_MAP.size(), SHARE_IMAGE_MAP.size());
    }

    @Override
    public CopyInfoDTO getPoint(int type) {
        if (ChatTypeEnum.CHAT.getValue() == type) {
            int rnd = (int) (Math.random() * POINT_CHAT_MAP.size());
            return POINT_CHAT_MAP.get(rnd);
        }
        int rnd = (int) (Math.random() * POINT_DRAW_MAP.size());
        return POINT_DRAW_MAP.get(rnd);
    }

    @Override
    public CopyInfoDTO getTips() {
        int rnd = (int) (Math.random() * TIPS_MAP.size());
        return TIPS_MAP.get(rnd);
    }

    @Override
    public CopyInfoDTO getShare() {
        // 获取分享内容
        int rnd = (int) (Math.random() * SHARE_CONTENT_MAP.size());
        CopyInfoDTO content = SHARE_CONTENT_MAP.get(rnd);
        // 获取分享图片
        rnd = (int) (Math.random() * SHARE_IMAGE_MAP.size());
        CopyInfoDTO image = SHARE_IMAGE_MAP.get(rnd);
        // 设置分享图片
        content.setImageUrl(image.getImageUrl());
        return content;
    }

    @Override
    public CopyInfoDTO getChatShare(String openId, long chatId, long chatContentId) {
        UserDTO user = userService.getUserInfo(openId);
        ChatContentDTO answer = chatContentMapper.queryById(user.getId(), chatId, chatContentId);
        if (null == answer) {
            return this.getShare();
        }
        ChatContentDTO question = null;
        if (null != answer.getQuestionId()) {
            question = chatContentMapper.queryById(user.getId(), chatId, answer.getQuestionId());
        }
        if (null == question) {
            return this.getShare();
        }
        CopyInfoDTO content = new CopyInfoDTO();
        content.setContent(question.getContent());
        content.setImageUrl(ChatService.IMAGE_PATH_THUMBNAIL + answer.getContent());
        return content;
    }

}


